
var turretRotationObject:GameObject;
var turretElevationObject:GameObject;
var turretBarrelsObject:GameObject;
var targetGameObject:GameObject;
var oldTargetInformation:Vector3;

var shooting:boolean = true;
var turretRange:int = 10;
var fireParticle:GameObject;

var turretRotation:Vector3 = new Vector3();

var bulletToFire:GameObject;
var bulletSpeed:float = 10;

var targetTurretRotation : Quaternion;

var health:float = 100;

var controllerObject:GameObject;
var controlScript:Controler;

function Start(){
	newTarget(GameObject.Find("Character"));
	//GameObject.Find("ControlContainer").GetComponent(FlattenVertexCircle).flattenVertexes(transform.position,5);
	fireParticle.active = false;
	
	controllerObject = GameObject.Find("ControlContainer");
	controlScript = controllerObject.GetComponent(Controler);
	
}

function newTarget(pTarget:GameObject){
	targetGameObject = pTarget;
}

function removeTarget(pTarget:GameObject){
	targetGameObject = null;
}

function fireABullet(){
	//Instantiate(Bullet, GetComponent("BarrelShootingParticles"), transform.rotation);
	var bulletClone:GameObject;
	//var bulletRotation:Vector3 = turretRotationObject.transform.rotation;
	//bulletRotation.x = turretElevationObject.transform.rotation.x;
	
	
	bulletClone = Instantiate(bulletToFire,fireParticle.transform.position,turretRotationObject.transform.rotation);
	
	bulletClone.transform.rotation.x = turretElevationObject.transform.rotation.x;
	
	bulletClone.GetComponent(BulletScript).startPosition = fireParticle.transform.position;
	bulletClone.rigidbody.velocity = turretRotationObject.transform.TransformDirection (Vector3.forward * bulletSpeed);
}

function Update () {
	
	if(health < ((health/100) * 75)){
		
	}
	
	
	
	if(targetGameObject != null){
		
			//Vector3.Distance(targetGameObject.position, transform.position
			var turretPositionDistanceCheck:Vector3 = transform.position;
			var targetPositionDistanceCheck:Vector3 = targetGameObject.transform.position;
			
			if(Vector3.Distance(turretPositionDistanceCheck, targetPositionDistanceCheck) < turretRange){
				
			
			
				// PROPPER TURRET ROTATION START
				var horizontalSeekSpeed:float = 1;
				
				var clampHorizontal : Vector3 = new Vector3(0, 1, 1);
				var clampVertical : Vector3 = new Vector3(1, 0, 1);
				var verticalSeekSpeed:float = 1;
				
				//Get the Quaternion value of the turret pointing at the target
			    var currentTurretRotation : Quaternion = turretRotationObject.transform.localRotation;
			    turretRotationObject.transform.LookAt(targetGameObject.transform.position);
			    
			    targetTurretRotation = turretRotationObject.transform.localRotation;
			    turretRotationObject.transform.localRotation = currentTurretRotation;
				
				// Clamp it to only y axis
			    currentTurretRotation.eulerAngles = Vector3.Scale(clampHorizontal, currentTurretRotation.eulerAngles);
			    targetTurretRotation.eulerAngles = Vector3.Scale(clampHorizontal, targetTurretRotation.eulerAngles);
				
				// At what angle will we be aimed at target?
			    //horizontalFiringSolution = targetTurretRotation;
				
				// Rotate it over time
				turretRotationObject.transform.localRotation = Quaternion.RotateTowards(currentTurretRotation, targetTurretRotation, horizontalSeekSpeed);
				
				// TURRET ROTATION END
				// BASE ROTATION DONE AND WORKING
			
			
			
			var quaternionValue:Quaternion;
				
				if(shooting && currentTurretRotation == targetTurretRotation){
					
					//WORKS BUT ONLY ROTATING (unhide turretRotation:Vector3)
					turretRotation.z += 20;
					quaternionValue = Quaternion.Euler(turretRotation);
					turretBarrelsObject.transform.localRotation = quaternionValue;
					fireABullet();
					fireParticle.active = true;
				} else {
					turretRotation.z = 0;
					quaternionValue = Quaternion.Euler(turretRotation);
					turretBarrelsObject.transform.localRotation = quaternionValue;
					fireParticle.active = false;
				}
			
			
			// START OF TURRET ELEVATION CODE
			
			var currentBarrelRotation : Quaternion;
		    var targetBarrelRotation : Quaternion;
		    
		    switch (2) {
		
		    case 1:
		        var solutionAngle = CalculateProjectileFiringSolution();
				//print("hello");
		        // Not enough range
		        if (solutionAngle != null) {
		            verticalFiringSolution = new Quaternion();
		            verticalFiringSolution.eulerAngles = new Vector3(solutionAngle, 0, 0);
		
		            currentBarrelRotation = turretElevationObject.transform.localRotation;
		            targetBarrelRotation = verticalFiringSolution;
		
		            turretElevationObject.transform.localRotation = Quaternion.RotateTowards(currentBarrelRotation, targetBarrelRotation, verticalSeekSpeed);
		        	
		        }
		        break;
		    case 2:
		        // Get the Quaternion value of the barrel pointing at the target
		        currentBarrelRotation = turretElevationObject.transform.localRotation;
		        turretElevationObject.transform.LookAt(targetGameObject.transform.position);
		        targetBarrelRotation = turretElevationObject.transform.localRotation;
		        turretElevationObject.transform.localRotation = currentBarrelRotation;
		
		        verticalFiringSolution = targetBarrelRotation;
		
		        // Clamp it to only the x axis
		        currentBarrelRotation.eulerAngles = Vector3.Scale(clampVertical, currentBarrelRotation.eulerAngles);
		        targetBarrelRotation.eulerAngles = Vector3.Scale(clampVertical, targetBarrelRotation.eulerAngles);          
		
		        // Rotate it over time
		        turretElevationObject.transform.localRotation = Quaternion.RotateTowards(currentBarrelRotation, targetBarrelRotation, verticalSeekSpeed);
		        //print("goodby");
		        break;
		    }
		 } else {
		 	fireParticle.active = false;
		 	getNewEnemy();
		 }
	 } else {
	 	getNewEnemy();
	 }
}

function getNewEnemy(){
	/*for(var i:int = 0; i < controlScript.enemyControler.enemyScript.length; i++){
		if(Vector3.Distance(controlScript.enemyScript[i].transform.position, transform.position) < turretRange)
		{
			targetGameObject = controlScript.enemyControler.enemyScript[i];
			break;
		} else {
			targetGameObject = null;
		}
	}*/
	
}

function CalculateProjectileFiringSolution() {
    var targetTransform =  targetGameObject.transform.position;
    var barrelTransform = turretBarrelsObject.transform.position;

    var y = barrelTransform.y - targetTransform.y;

    targetTransform.y = barrelTransform.y = 0;
	
	// custom var
	var projectileSpeed:float = 1;
	
    var x = (targetTransform - barrelTransform).magnitude;
    var v = projectileSpeed;
    var g = Physics.gravity.y;

    var sqrt = (v*v*v*v) - (g * (g * (x*x) + 2 * y * (v*v)));
print(1);
    // Not enough range
    if (sqrt < 0) {
        return null;
    }

    sqrt = Mathf.Sqrt(sqrt);
print(2);
    // DirectFire chooses the low trajectory, otherwise high trajectory.
    if (false) {  
    print(3); 
        return Mathf.Atan(((v*v) - sqrt) / (g*x)) * Mathf.Rad2Deg;
    } else {
        return Mathf.Atan(((v*v) + sqrt) / (g*x)) * Mathf.Rad2Deg;
    }
}
